﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuizAppMvc2.Models;

namespace QuizAppMvc2.Controllers
{
    public class SubtopicoController : Controller
    {
        //
        // GET: /Subtopico/
        quizdbEntities _db = new quizdbEntities();

        public ActionResult Index()
        {
            return View(_db.SubTopicoes);
        }

        //
        // GET: /Subtopico/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Subtopico/Create

        public ActionResult Create()
        {
            SubTopicoModels subtopico = new SubTopicoModels();
            subtopico.materias = _db.Materias.ToList<Materia>();
            return View(subtopico);
        } 

        //
        // POST: /Subtopico/Create

        [HttpPost]
        public ActionResult Create(SubTopicoModels collection)
        {
            try
            {
                SubTopico sub = new SubTopico();
                sub.idMateria = collection.idMateria;
                sub.idTopico = collection.idTopico;
                sub.Nome = collection.texto;

                _db.AddToSubTopicoes(sub);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Subtopico/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Subtopico/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Subtopico/Delete/5
 
        public ActionResult Delete(int id)
        {
            var toDelete = (from m in _db.SubTopicoes

                            where m.idSubTopico == id

                            select m).First();

            return View(toDelete);
        }

        //
        // POST: /Subtopico/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var toDelete = (from m in _db.SubTopicoes

                                where m.idSubTopico == id

                                select m).First();

                _db.DeleteObject(toDelete);
                _db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult abc(int year)
        {
            var v = _db.Topicoes.Where(x => x.idMateria == year);
            //var v = db.Topicoes;

            return Json(
                v.Select(x => new { value = x.idTopico, novo = x.Nome }),
                JsonRequestBehavior.AllowGet
            );
        }

        public ActionResult Topicos(int idMateria)
        {
            quizdbEntities db = new quizdbEntities();
            var v = _db.Topicoes.Where(e => e.idMateria == idMateria);
            return Json(v, JsonRequestBehavior.AllowGet);
            //return Json(
            //    Enumerable.Range(1, 12).Select(x => new { value = x, text = x }),
            //    JsonRequestBehavior.AllowGet
            //);
        }
    }
}
